// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database();
const _ = db.command;
const suffererOrbit = db.collection("suffererOrbit");
const myOrbit = db.collection("myOrbit"); 

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext();
  var city =event.city;
  var countResult = await suffererOrbit
                    .where({
                      city:city
                    }).count();
  var total = countResult.total;
  var batchTimes = Math.ceil(total / 100);
  var data =[];
  var page =0;
  for (var i=0;i<batchTimes;i++){
    var d = await suffererOrbit
    .where({
      city:city
    }).orderBy('date', 'desc').skip(page).get();
    data = data.concat(d.data);
    page+=100;
  }


  var countMy = await myOrbit
                    .where({
                      city:city
                    }).count();
  var myTotal = countMy.total;
  var myBatchTimes = Math.ceil(myTotal / 100);
  var my =[];
  var page =0;
  for (var i=0;i<myBatchTimes;i++){
    var d = await myOrbit
    .where({
      _openid:wxContext.OPENID,
      city:city
    }).orderBy('date', 'desc').skip(page).get();
    my = my.concat(d.data);
    page+=100;
  }

  return {
   data:data,
   my:my,
  }
}